//
//  InitiationWaitViewController.m
//  Molian
//
//  Created by xiaocankeji on 16/1/29.
//  Copyright © 2016年 xiaocan. All rights reserved.
//

#import "InitiationWaitViewController.h"
#import "InitiationPayViewController.h"
#import "InitiationFailViewController.h"
#import "InitiationFreeViewController.h"

@implementation InitiationWaitViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self.navigationItem setHidesBackButton:YES animated:YES];
    self.navigationItem.rightBarButtonItem = nil;
    self.navigationItem.leftBarButtonItem = nil;
    self.title = @"入会审核";
    self.view.backgroundColor =  APPCOLORBACKGROUNDCOLOR(1);
    CGFloat h = DeviceWidth/320*180;
    
    CGFloat start = kNavigationBarHeight;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,start , DeviceWidth, h)];
    v.backgroundColor = RGBA(55, 198, 119, 1);
//    v.backgroundColor = RGBA(55, 149, 202, 1);
//    v.backgroundColor = RGBA(244, 152, 68, 1);
//    v.backgroundColor = RGBA(243, 93, 67, 1);
    [self.view addSubview:v];
    
    UIImageView *i = [[UIImageView alloc] initWithFrame:CGRectMake(DeviceWidth/2 - 50/2, 20, 50, 50)];
    i.image = [UIImage imageNamed:@"data_icon"];
    [v addSubview:i];
    
    
    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, DeviceWidth - 20, h - 70)];
    l.numberOfLines = 0;
    l.textColor = [UIColor whiteColor];
    l.textAlignment = NSTextAlignmentCenter;
    l.font = kFont(15);
    l.text = @"您的资料已经顺利提交了\n请等待审核结果";
    [v addSubview:l];
    
    
    
    UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(10, h + start, DeviceWidth - 20, 70)];
    l2.numberOfLines = 0;
    l2.textColor = RGBA(133, 133, 133, 1);
    l2.textAlignment = NSTextAlignmentLeft;
    l2.font = kFont(15);
    l2.text = @"我们将在1~2个工作日内完成审核, 通过后您将收到短信通知, 请及时完成支付";
    [self.view addSubview:l2];
   
    
    
    
    
    UIButton *downBt = [UIButton buttonWithType:UIButtonTypeCustom];
    downBt.frame = CGRectMake(0, DeviceHeight - 44, DeviceWidth, 44);
    downBt.backgroundColor = [UIColor whiteColor];
    [downBt setImage:[UIImage imageNamed:@"refresh_icon"] forState:UIControlStateNormal];
    [downBt setTitle:@"刷新审核结果" forState:UIControlStateNormal];
   // [downBt setImageEdgeInsets:UIEdgeInsetsMake(-2,-18, -2, 0)];
    //[downBt setTitleEdgeInsets:UIEdgeInsetsMake(0, -12, 0, -10)];
    [downBt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
    [downBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
    downBt.titleLabel.font = kFont(15);
    [downBt addTarget:self action:@selector(clickDownBt:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:downBt];
    
    
    
    
}
- (void)clickDownBt:(UIButton *)bt
{
    bt.enabled = NO;
    AVQuery *q =[MLAuthentication query];
    [q whereKey:@"sponsor" equalTo:[MLUser currentUser]];
    [q whereKey:@"needRecommend" equalTo:[NSNumber numberWithInteger:MLAuthenticationNeedRecommend_Initiation]];
    [q orderByDescending:@"createdAt"];
    // [q whereKey:@"status" equalTo:[NSNumber numberWithInteger:AuthenticationStatus_APPLYING]];
    [HUD setOnViewWithTitle:nil animated:YES];
    [q getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        
        [HUD hideFromViewAnimated:YES];
        if (error == nil) {
            MLAuthentication *auth = (MLAuthentication *)object;
            if (auth == nil) {
                bt.enabled = YES;
            }else{
                if (auth.status == AuthenticationStatus_APPLYING) {
                  
                     [HUD Prompt:@"正在审核中,请耐心等待"];
                    //等待审核
                    bt.enabled = YES;
                    
                    
                    
                }else if(auth.status == AuthenticationStatus_ACCEPTED){
                    //申请成功
                    //申请成功
                    //支付
                    __block  MLUser *u = [MLUser currentUser];
                    [u refreshInBackgroundWithBlock:^(AVObject *object, NSError *error) {
                        if (!error) {
                            u = (MLUser *)object;
                            if (u.isPay) {
                                InitiationPayViewController *init = [[InitiationPayViewController alloc] init];
                                init.hidesBottomBarWhenPushed = YES;
                                [self.navigationController pushViewController:init animated:YES];
                            }else{
                                InitiationFreeViewController *init = [[InitiationFreeViewController alloc] init];
                                init.hidesBottomBarWhenPushed = YES;
                                [self.navigationController pushViewController:init animated:YES];
                            }
                            
                            
                        }else{
                            InitiationPayViewController *init = [[InitiationPayViewController alloc] init];
                            init.hidesBottomBarWhenPushed = YES;
                            [self.navigationController pushViewController:init animated:YES];
                        }
                    }];
                    
                    
                    
                }else if(auth.status == AuthenticationStatus_REJECT){
                    //申请失败
                    
                    InitiationFailViewController *init = [[InitiationFailViewController alloc] init];
                    init.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:init animated:YES];
                }
                
                
                
            }
            
            
        }else{
            
            
            bt.enabled = YES;
            
        }
        
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}
@end
